package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.TransTypeEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.TransFlowService;
import com.atguigu.srb.core.service.UserAccountService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.atguigu.srb.mq.pojo.dto.SmsDTO;
import com.atguigu.srb.mq.service.MqService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户账户 服务实现类
 * </p>
 *
 * @author Mr.bilal
 * @since 2023-07-18
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Autowired
    TransFlowService transFlowService;

    @Autowired
    MqService mqService;

    @Override
    public String commitCharge(String userId, String chargeAmt) {

        UserInfo userInfo = userInfoMapper.selectById(userId);
        Map<String,Object> paramMap = new HashMap<>();
        //给商户分配的唯一标识
        paramMap.put("agentId",HfbConst.AGENT_ID);
        //生成单号
        String chargeNo = LendNoUtils.getChargeNo();
        paramMap.put("agentBillNo",chargeNo);
        paramMap.put("bindCode",userInfo.getBindCode());
        //充值金额，即充值到汇付宝的金额。支持小数点后2位。
        paramMap.put("chargeAmt",new BigDecimal(chargeAmt));
        //手续费
        paramMap.put("feeAmt",new BigDecimal("0"));
        //通知商户充值成功的完整地址,就是汇付宝操作完成后回调尚融宝接口地址
        paramMap.put("notifyUrl",HfbConst.RECHARGE_NOTIFY_URL);
        // 返回页面的路径
        paramMap.put("returnUrl",HfbConst.RECHARGE_RETURN_URL);
        //时间戳
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        //签名
        paramMap.put("sign",RequestHelper.getSign(paramMap));

        String form = FormHelper.buildForm(HfbConst.RECHARGE_URL, paramMap);

        return form;
    }

    @Override
    public void notifyCommitCharge(Map<String, Object> stringObjectMap) {
        //充值人绑定协议号
        String bindCode = (String)stringObjectMap.get("bindCode");
        //商户充值订单号
        String chargeNo = (String)stringObjectMap.get("agentBillNo");
        //充值金额
        String chargeAmt = (String)stringObjectMap.get("chargeAmt");

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bind_code",bindCode);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        Long userId = userInfo.getId();

        // 修改userAccount，amount帐户可用余额和freeze_amount冻结金额
        UpdateWrapper<UserAccount> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId)
                .setSql("amount = amount +"+chargeAmt+",freeze_amount = freeze_amount +"+0);
        baseMapper.update(null,updateWrapper);

        // 记录交易流水
        TransFlow transFlow = new TransFlow();
        transFlow.setTransNo(chargeNo);
        transFlow.setTransTypeName(TransTypeEnum.RECHARGE.getTransTypeName());
        transFlow.setUserName(userInfo.getName());
        transFlow.setTransType(TransTypeEnum.RECHARGE.getTransType());
        transFlow.setTransAmount(new BigDecimal(chargeAmt));
        transFlow.setUserId(userId);
        transFlow.setMemo("充值到账");
        transFlowService.save(transFlow);

        SmsDTO smsDTO = new SmsDTO();
        smsDTO.setMobile(userInfo.getMobile());
        smsDTO.setMessage("ok");
        mqService.sendMessage("exchange.recharge.sms","routing.recharge.sms",smsDTO);
    }

    @Override
    public BigDecimal getUserAccountByUserId(String userId) {
        UserAccount userAccount = baseMapper.selectById(userId);
        if (userAccount == null) {
            return new BigDecimal("0");
        }
        return userAccount.getAmount();
    }

    @Override
    public UserAccount getUserAccountById(String userId) {
        QueryWrapper<UserAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserAccount userAccount = baseMapper.selectOne(wrapper);
        return userAccount;
    }
}
